package com.fallsvc.dayproblem.demo202511;

import java.util.Arrays;
import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.Set;

/**
 * @auther falls_vc
 * description:
 * @date 2025/11/12  15:44
 */
public class Demo20251112 {
    public static void main1(String[] args) {
        PriorityQueue<Integer> queue = new PriorityQueue<>();
        queue.offer(1);
        queue.offer(2);
        queue.offer(1);
        while (!queue.isEmpty()) {
            System.out.println(queue.poll());
        }
        Set<Integer> set =  new HashSet<>();
        set.add(1);set.add(2);set.add(1);
        System.out.println(set);
        for (Integer x : set) {
            System.out.println(x);
        }
    }

    public static void main(String[] args) {
        int[] nums = {3,2,20,1,1,3};
        minOperations(nums,10);
    }
    public static int minOperations(int[] nums, int x) {
        // 转化为找中间区间为allSum - x
        int allSum = 0;
        for (int num : nums ){
            allSum += num;
        }
        if(x > allSum) return -1;

        // 可能存在
        int l = 0,r = 0;
        int sum = nums[0];
        int ret = -1; // 中间最大长度
        int target = allSum - x; // 目标值

        while(r < nums.length) {
            if(sum > target) {
                sum -= nums[l];
                l++;
            }else {
                if(sum == target)
                    ret =Math.max(ret, r - l + 1);
                r++;
                if(r <nums.length)
                    sum += nums[r];
            }
        }

        return ret == -1 ? ret : nums.length - ret;
    }
    public int minOperations2(int[] nums, int x) {
        int len = -1;// 返回数 等下找中间数的最大长度
        int n = nums.length;
        int sum = 0; // 中间数和
        int allSum = 0; //所有数之和
        for (int num : nums) {
            allSum += num;
        }

        int target = allSum - x;
        // 1判断是否整个数组之和<x
        if (target < 0) {
            return len;
        }
        // 2计算中间数之和是否等于allSum-x
        for (int left = 0, right = 0; right < n; right++) {
            sum += nums[right];// 入窗口
            while (sum > target) {// 判断
                sum -= nums[left++];//出窗口
            }
            if (sum == target) {
                len = Math.max(len, right - left + 1);
            }
        }
        return len == -1 ? len : n - len;
    }



//    https://leetcode.cn/problems/third-maximum-number/
//    第三大元素
    public int thirdMax(int[] nums) {
        // 小根堆存储前三数字
        PriorityQueue<Integer> queue = new PriorityQueue<>();
        for(int i = 0; i<nums.length; i++) {
            if(!queue.contains(nums[i])) {
                if(queue.size() < 3){
                    queue.offer(nums[i]);
                }else {
                    if(nums[i] > queue.peek()){
                        queue.poll();
                        queue.offer(nums[i]);
                    }
                }
            }

        }
        if(queue.size() < 3) {
            while(queue.size() > 1) {
                queue.poll();
            }
        }
        return queue.poll();
    }
    public int thirdMax1(int[] nums) {
        Arrays.sort(nums);
        reverse(nums);
        for (int i = 1, diff = 1; i < nums.length; ++i) {
            if (nums[i] != nums[i - 1] && ++diff == 3) { // 此时 nums[i] 就是第三大的数
                return nums[i];
            }
        }
        return nums[0];
    }

    private void reverse(int[] nums) {
        int l = 0;
        int r = nums.length - 1;
        while (l < r) {
            int tmp = nums[l];
            nums[l] = nums[r];
            nums[r] = tmp;
            l++;
            r--;
        }
    }


}
